<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------

namespace app\wh\model;

use think\Model;
use app\admin\model\Config as ConfigModel;
use app\wh\model\Stock as StockModel;
use app\wh\model\Material as MaterialModel;
use think\Db;

/**
 * 厂家颜色表模型
 */
class Color extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $name = 'mill_order_sample';

    // 自动写入时间戳
    protected $autoWriteTimestamp = true;

    //状态
    public static $status = [
        -1 =>     '未通过',
        0 =>      '待提交',
        1 =>      '待审核',
        2 =>      '已审核',
    ];
    //颜色类型
    public static $type_pid_arr = [
        //1,2,8,9,10,12
        1,8,9,10,12
    ];
    //可定色的颜色类型
    public static $type_pid_c_arr = [
        1,9,10,12
    ];
    //颜色类型,无油温
    public static $type_pid_not_heat_arr = [
        2,12
    ];
    // 设置自编码
    public static function getColorSn($type_id = 0,$heat = 0){

        //物料类型
        $stock_type = Db::name('wh_stock_type')->where(['id'=>$type_id])->find();
        //获取自定义编码
        $code = $stock_type['code'];
        if(empty($code))$code = 'ZBM';
        $config = new ConfigModel();
        //分配自编码序号
        $sort_arr = $config->getConfig('wh_color_sn');
        $sort = empty($sort_arr[$code])?1:$sort_arr[$code];
        $sort_arr[$code] = $sort+1;
        //更新颜色自编码
        ConfigModel::where('name', 'wh_color_sn')->update(['value' =>implode_attr($sort_arr)]);
        //是否附加油温编码
        if($stock_type['is_heat_code'] == 1)$code .= MaterialModel::$heat_code[intval($heat)];

        return $code.sprintf ("%06d", $sort);
    }
    //获取厂家色号库存
    public static function getSupplierColorNum($color_id = 0){

        $where = [
            ['id','=',$color_id]
        ];

        $info = Db::name('wh_supplier_color')
            ->field('title as model,supplier_id,type_id')
            ->where($where)
            ->find();

        $num = StockModel::getSumNumber($info);
        return $num;
    }
    //获取油漆类型
    public static function getColorType()
    {
        $map['status'] = 1;
        $data = Db::name('wh_supplier_color_type')->where($map)->column('title','id');
        return $data;
    }
    //获取材质类型
    public static function getQuality($type_pid = 0)
    {
        $map['status'] = 1;
        if(!empty($type_pid))$map['type_pid'] = $type_pid;
        $data = Db::name('wh_supplier_quality')->where($map)->column('title','id');
        return $data;
    }
    //总面积换算成采购量
    public static function areaToKg($row_area = 0){

        $kg = $row_area;
        return $kg;
    }

    /**
     * 同步新增同规格不同物料类别的库存
     * @param int $color_id 定色ID
     * @return array
     */
    public static function addSameColor($color_id = 0){

        $info = Db::name('wh_order_color')->where(['id'=>$color_id])->find();
        //同步更新覆盖率信息
        if(!empty($info['stock_id'])){
            $goods_update = [
                'supplier_id'       =>  $info['supplier_id'],
                'model'             =>  $info['supplier_color'],
                'type_pid'          =>  $info['type_pid'],
                'type_id'           =>  $info['type_id'],
                'type_id2'          =>  $info['type_id2'],
                'type_id3'          =>  $info['type_id3'],
                'stock_id'          =>  $info['stock_id'],
                'stock_sn'          =>  $info['stock_sn'],
            ];
            //Db::name('wh_material_goods')->where(['order_color_id'=>$add_id])->update($goods_update);
            Db::name('wh_stock_use_log')->where(['order_color_id'=>$color_id])->update($goods_update);
        }
        //暂时关闭功能
        return '';
        //目前仅限稀油和油漆的同步新增

        //判断是否有已同步的定色信息
        $new_info = Db::name('wh_order_color')->where(['link_order_color_id'=>$info['id']])->find();

        //获取已同步的物料信息
        $new_stock_sn = StockModel::addSameStock($info['stock_sn']);
        //若有关联的物料信息，则新增同步的定色信息
        if(!empty($new_stock_sn)){
            $new_stock = Db::name('wh_stock')->where(['stock_sn'=>$new_stock_sn])->find();

            $add = $info;
            $add['supplier_id'] = $new_stock['supplier_id'];
            $add['supplier_color'] = $new_stock['supplier_color'];
            $add['type_pid'] = $new_stock['type_pid'];
            $add['type_id'] = $new_stock['type_id'];
            $add['type_id2'] = $new_stock['type_id2'];
            $add['type_id3'] = $new_stock['type_id3'];
            $add['stock_id'] = $new_stock['id'];
            $add['stock_sn'] = $new_stock['stock_sn'];
            $add['heat'] = $new_stock['heat'];
            $add['color_type_id'] = $new_stock['color_type_id'];
            $add['color_sn'] = $new_stock['color_sn'];
            $add['link_order_color_id'] = $info['id'];

            unset($add['id']);
        }

        if(!empty($add)){
            //新增同步的定色信息
            if(empty($new_info)){
                $new_info['id'] = Db::name('wh_order_color')->insertGetId($add);
                $new_info = Db::name('wh_order_color')->where(['id'=>$new_info['id']])->find();
            }
            //更新同步的定色信息
            else Db::name('wh_order_color')->where(['id'=>$new_info['id']])->update($add);

            //同步更新覆盖率信息
            if(!empty($new_info['stock_id'])){
                $goods_update = [
                    'supplier_id'       =>  $new_info['supplier_id'],
                    'model'             =>  $new_info['supplier_color'],
                    'type_pid'          =>  $new_info['type_pid'],
                    'type_id'           =>  $new_info['type_id'],
                    'type_id2'          =>  $new_info['type_id2'],
                    'type_id3'          =>  $new_info['type_id3'],
                    'stock_id'          =>  $new_info['stock_id'],
                    'stock_sn'          =>  $new_info['stock_sn'],
                ];
                //Db::name('wh_material_goods')->where(['order_color_id'=>$add_id])->update($goods_update);
                Db::name('wh_stock_use_log')->where(['order_color_id'=>$new_info['id']])->update($goods_update);
            }
        }

        return '';
    }

    // 表格制作
    //print 0:编辑模式 1:打印模式
    public static function fromTabale($data = [],$print = 0){

        $table_head = [["[13:1]{$data['engineering']}  {$data['archives_sn']}"]];
        $table_data[] = [
            "<div style='text-align: center;'>工程名称</div>",
            "<div style='text-align: center;'>厂商色号</div>",
            "<div style='text-align: center;'>颜色名称</div>",
            "<div style='text-align: center;'>供应商</div>",
            "<div style='text-align: center;'>油漆类别</div>",
            "<div style='text-align: center;'>类别</div>",
            //"<div style='text-align: center;'>类别2</div>",
            //"<div style='text-align: center;'>类别3</div>",
            "<div style='text-align: center;'>油温</div>",
            "<div style='text-align: center;'>油漆类型</div>",
            "<div style='text-align: center;'>面积</div>",
            "<div style='text-align: center;'>数量</div>",
            "<div id='color_note'><div style='text-align: center;float: left;margin: 7px auto;'>自编码</div>
<a style='display: none;float: right;' class='btn btn-success' onclick=\"color_add()\" ><i class='fa fa-plus-circle'></i></a>
</div>",
        ];

        //供应商的下拉选择
        $supplier_option = '<option></option>';
        foreach ($data['supplier'] as $suk=>$suv){

            $supplier_option .= '<option value="'.$suk.'">'.$suv.'</option>';
        }

        foreach ($data['color'] as $sak=>$sav){
            $new_supplier_option = str_replace('"'.$sav['supplier_id'].'"','"'.$sav['supplier_id'].'" selected',$supplier_option);
            if(!empty($print)){
                $table_data[] = [
                    "<div style='text-align: center;'>{$data['engineering']}</div>",
                    "<div style='text-align: center;'>{$sav['color_sn']}</div>",
                    "<div style='text-align: center;'>{$sav['color']}</div>",
                    "<div style='text-align: center;'>{$sav['supplier_color']}</div>",
                    "<div style='text-align: center;'>{$sav['supplier']}</div>",
                ];
            }
            else{

                $is_suppliers = false;
                if(!empty($sav['id']))$is_suppliers = true;

                $table_data[] = [
                    "[0]
                    <input class='form-control color' type='hidden' name='color[id][]' value='{$sav['id']}'>
                    <input class='form-control color' type='hidden' name='color[archives_sn][]' value='{$sav['archives_sn']}'>
                    <input class='form-control color' type='hidden' name='color[order_id][]' value='{$sav['order_id']}'>
                    <input class='form-control color' type='hidden' name='color[stock_id][]' value='{$sav['stock_id']}'>
                    <input class='form-control color' type='hidden' name='color[stock_sn][]' value='{$sav['stock_sn']}'>
                    <input class='form-control color' type='hidden' name='color[row_area][]' value='{$sav['row_area']}'>
                    <input class='form-control color' type='hidden' name='color[archives_number][]' value='{$sav['archives_number']}'>
                    <input class='form-control color' type='hidden' name='color[archives_color][]' value='{$sav['archives_color']}'>
                    
                    <input class='form-control color' type='hidden' name='color[color][]' value='{$sav['color']}'>
                    <input class='form-control color' type='hidden' name='color[supplier_id][]' value='{$sav['supplier_id']}'>
                    <input class='form-control color' type='hidden' name='color[type_pid][]' value='{$sav['type_pid']}'>
                    <input class='form-control color' type='hidden' name='color[type_id][]' value='{$sav['type_id']}'>
                    <input class='form-control color' type='hidden' name='color[type_id2][]' value='{$sav['type_id2']}'>
                    <input class='form-control color' type='hidden' name='color[type_id3][]' value='{$sav['type_id3']}'>
                    <input class='form-control color' type='hidden' name='color[heat][]' value='{$sav['heat']}'>
                    <input class='form-control color' type='hidden' name='color[color_type_id][]' value='{$sav['color_type_id']}'>
                    <input class='form-control color' type='hidden' name='color[color_sn][]' value='{$sav['color_sn']}'>
                    {$data['engineering']}",
                    "[0]
                    ".MaterialModel::inputToText('color[supplier_color][]',$sav['supplier_color'],$print,'display:inline;width: 70%;','input','color').
                    "<a class='btn btn-success supplier-color color disable-dch pop' data-href='".url('supplierColorList',['_pop'=>1,'_parent_i'=>'__parent_i__','_parent_color_title'=>urlencode($sav['archives_color'])])."' data-layer=\"{&quot;area&quot;:[&quot;90%&quot;,&quot;95%&quot;],&quot;title&quot;:[&quot;请选择厂家色号&quot;,&quot;font-size:24px;text-align: center;&quot;]}\" onclick=\"supplier_color_list(this)\">查询</a>",
                    "[0]
                    ".MaterialModel::inputToText('show[color][]',$sav['color'],$print,'','input','color disabled','',$sav['archives_color']),
                    "[0]
                    ".MaterialModel::inputToText('show[supplier_title][]',$sav['supplier_title'],$print,'','input','color disabled','','',['width'=>'170px']),
                    "[0]
                    ".MaterialModel::inputToText('show[type_ptitle][]',$sav['type_ptitle'],$print,'','input','color disabled','','',['width'=>'130px']),
                    "[0]
                    ".MaterialModel::inputToText('show[type_title][]',$sav['type_title'],$print,'','','color disabled','','',['width'=>'130px']),
                    //"[0]".MaterialModel::inputToText('show[type_title2][]',$sav['type_title2'],$print,'','','color disabled','','',['width'=>'130px']),
                    //"[0] ".MaterialModel::inputToText('show[type_title3][]',$sav['type_title3'],$print,'','','color disabled','','',['width'=>'130px']),
                    "[0]
                    ".MaterialModel::inputToText('show[heat_title][]',$sav['heat_title'],$print,'','','color disabled','','',['width'=>'100px']),
                    "[0]
                    ".MaterialModel::inputToText('show[color_type_title][]',$sav['color_type_title'],$print,'','','color disabled','','',['width'=>'100px']),
                    "[0]
                    ".MaterialModel::inputToText('color[row_area2][]',$sav['row_area2'],$print,'width:90px','input','color disabled'),
                    "[0]
                    ".MaterialModel::inputToText('color[archives_number2][]',$sav['archives_number2'],$print,'width:80px','input','color disabled'),
                    "[0]
                    ".MaterialModel::inputToText('show[color_sn][]',$sav['color_sn'],$print,'display:inline;width: 50%;','input','color disabled')."
                    <a class='btn btn-success' onclick=\"color_add(this)\" ><i class='fa fa-plus-circle'></i></a>
                    <a class='btn btn-danger color color_del' onclick=\"color_del(this)\" ><i class='fa fa-times'></i></a>
                    ",
                    ];
            }

        }

        return ['head'=>$table_head,'data'=>$table_data];
    }
}
